using System;
using System.Collections.Generic;
using System.Text;
using Ltf.MonitorLizard.Forms;
using System.Windows.Forms;
using System.Drawing;

namespace ExtendedControls
{
	public class ExtendedVirtualViewItem : VirtualViewItem
	{
	private ExtendedVirtualViewItem _parentItem;
		private int _depth;
		private ExtendedListView _extendedListView;
		internal List<ExtendedControls.ExtendedListViewItem.DepthMarks> DepthMarkers;

		internal Guid ChildCheckStateRecalcGuid = Guid.NewGuid();
		internal FolderCheckState FolderCheckState;

    
    public ExtendedVirtualViewItem(ExtendedListView view):base() 
		{
			this.ExtendedListView = view; 
      Children = new ExtendedListView.ExtendedVirtualViewItemCollection(this.ExtendedListView,this);
			DepthMarkers = new List<ExtendedControls.ExtendedListViewItem.DepthMarks>();
		}

		public ExtendedVirtualViewItem(ExtendedListView view, ExtendedListViewItem extLVItem)
			: this(view)
		{
			Tag = extLVItem.Tag;
			ImageKey = extLVItem.ImageKey;
			SecondaryImageKey = extLVItem.SecondaryImageKey;
			SecondaryImageIndex = extLVItem.SecondaryImageIndex;
			HasCheckbox = extLVItem.HasCheckbox;
			IsExpanded = extLVItem.IsExpanded;
			Depth = extLVItem.Depth;
			if (extLVItem.Group!=null)
				Group = extLVItem.Group;
			ForeColor = extLVItem.ForeColor;
			ParentItem = extLVItem.ParentItem;
			Checked = extLVItem.Checked;
			extLVItem.VirtualItem = this;
			List<string> itemsList = new List<string>();
			foreach (ListViewItem.ListViewSubItem sub in extLVItem.SubItems)
			{
				itemsList.Add(sub.Text);
			}
			this.Items = itemsList.ToArray();
		}


    public ExtendedListView.ExtendedVirtualViewItemCollection Children;
		public Dictionary<int, Bitmap> Bitmaps = new Dictionary<int, Bitmap>();

		public string Location;
		public bool IsFolder=false;
		public bool hasDropRect = false;
		public int key = 0;

    public ExtendedVirtualViewItem ParentItem {
      get { return _parentItem; }
      internal set { _parentItem=value; }
    }

		private bool isRedrawRequired;

		public bool IsRedrawRequired
		{
			get { return isRedrawRequired; }
			set 
			{
				isRedrawRequired = value;
				if (ParentItem != null)
					ParentItem.IsRedrawRequired = value;
			}
		}


    public bool Visible {
      get {
        if (_parentItem==null) {
          return true;
        }
        return (_parentItem.Visible & _parentItem.IsExpanded);
      }
    }

		public ExtendedListView ExtendedListView
		{
      get { return _extendedListView; }
      internal set { _extendedListView = value; }
    }


    public int Depth {
      get { return _depth; }
      internal set { _depth = value; }
    }

    bool _isExpanded = false;
    
    public bool IsExpanded {
      get { return _isExpanded; }
      internal set { _isExpanded = value; }
    }

		private string secondaryImageKey=null;

		public string SecondaryImageKey
		{
			get { return secondaryImageKey; }
			set { secondaryImageKey = value; }
		}

		private int secondaryImageIndex=-1;

		public int SecondaryImageIndex
		{
			get { return secondaryImageIndex; }
			set { secondaryImageIndex = value; }
		}

		private bool hasCheckbox=false;

		public bool HasCheckbox
		{
			get { return hasCheckbox; }
			set { hasCheckbox = value; }
		}

		public bool IsLastChild
		{
			get
			{
				return this == this.ParentItem.Children.Last;
			}
		}

		public ExtendedListViewItem ConvertToExtendedListViewItem()
		{
			ExtendedListViewItem elvi = new ExtendedListViewItem(base.Items);
			elvi.Tag = Tag;
			elvi.ImageKey = ImageKey;
			elvi.SecondaryImageKey = SecondaryImageKey;
			elvi.SecondaryImageIndex = SecondaryImageIndex;
			elvi.HasCheckbox = hasCheckbox;
			elvi.IsExpanded = this.IsExpanded;
			elvi.Depth = this.Depth;
			elvi.Group = this.Group;
			elvi.ForeColor = this.ForeColor;
			elvi.ChildCount = this.Children.Count;
			elvi.ParentItem = this.ParentItem;
			elvi.VirtualItem = this;
			elvi.Checked = this.Checked;
			return elvi;
		}

		public IEnumerable<ExtendedVirtualViewItem> CheckedDescendants
		{
			get
			{
				foreach (ExtendedVirtualViewItem child in Children)
				{
					if (child.Checked)
						yield return child;
					foreach (ExtendedVirtualViewItem desc in child.CheckedDescendants)
						yield return desc;
				}
			}
		}

		public IEnumerable<ExtendedVirtualViewItem> AllDescendants
		{
			get
			{
				foreach (ExtendedVirtualViewItem child in Children)
				{
					yield return child;
					foreach (ExtendedVirtualViewItem desc in child.AllDescendants)
						yield return desc;
				}
			}
		}

		public FolderCheckState GetFolderCheckState()
		{
			int checkedCount=0;
			int uncheckedCount = 0;
			int nobox = 0;
			foreach (ExtendedVirtualViewItem evvi in Children)
			{
				if (evvi.IsFolder && ChildCheckStateRecalcGuid!=ExtendedListView.ChildCheckStateRecalcGuid)
				{
					evvi.FolderCheckState = evvi.GetFolderCheckState();
					evvi.ChildCheckStateRecalcGuid = ExtendedListView.ChildCheckStateRecalcGuid;
				}
				if (evvi.HasCheckbox)
				{
					if (evvi.Checked)
						checkedCount++;
					else
						uncheckedCount++;
				}
				else
				{
					switch (evvi.FolderCheckState)
					{
						case FolderCheckState.Checked:
							checkedCount++;
							break;
						case FolderCheckState.Unchecked:
							uncheckedCount++;
							break;
					}
				}
			}
			if (checkedCount >= Children.Count-nobox && checkedCount!=0)
				return FolderCheckState.Checked;
			else if (uncheckedCount >= Children.Count-nobox)
				return FolderCheckState.Unchecked;
			else
				return FolderCheckState.PartiallyChecked;
		}
  }
}


